package com.example.yiwei.crm.view.activity.healthActivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.view.Class.listviewClass.ParticipationListAdapter;
import com.example.yiwei.crm.view.Class.listviewClass.managerSelectListAdapter;

import java.util.List;

import static com.example.yiwei.crm.data.GetDockData.dockingEntityList;

public class participationActivity extends AppCompatActivity {
private ListView listView;
    private ImageView img_back,img_commit;
    private TextView txt_title;
    List<dockingEntity> dockingEntityList;
    ParticipationListAdapter ladapter;
    String participation_name="";
    String manager_name;
    String participation_id="";
    dockingEntity dockingEntity;
    int manager_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_participation);
        listView= (ListView) findViewById(R.id.participation_select_listview);
        img_back= (ImageView) findViewById(R.id.participation_select_img_back);
        img_commit= (ImageView) findViewById(R.id.participation_select_img_commit);
        txt_title= (TextView) findViewById(R.id.participation_select_txt_people);
        if ("participation".equals(getIntent().getStringExtra("tag"))){
            txt_title.setText("选择参与人员");
        }else if ("manager".equals(getIntent().getStringExtra("tag"))){
            txt_title.setText("选择负责人员");
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                dockDao dockDao=new dockDaoImpl();
                dockingEntityList=dockDao.queryManagerSpinnerMessage(baseClass.query_manager_url,null);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ladapter = new ParticipationListAdapter(participationActivity.this,dockingEntityList);
                        listView.setAdapter(ladapter);
                        ladapter.notifyDataSetChanged();

                    }
                });
            }
        }).start();
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("participation".equals(getIntent().getStringExtra("tag"))){
                    final List<String> list2 = ParticipationListAdapter.getNames();
                    final List<Integer> list3=ParticipationListAdapter.getIDs();
                    for (int i=0;i<list2.size();i++){
                        participation_name=list2.get(i)+","+participation_name;
                    }
                    for (int i=0;i<list3.size();i++){
                        participation_id=list3.get(i)+","+participation_id;
                    }
                    GetDock.dockingEntity2.setParticipation_name(participation_name);
                    GetDock.dockingEntity2.setParticipation_id(participation_id);
                    if (GetDock.dockingEntity2!=null){
                        finish();
                    }
                }else if ("manager".equals(getIntent().getStringExtra("tag"))){
                    final List<String> list3 = ParticipationListAdapter.getNames();
                    final List<Integer> list4 = ParticipationListAdapter.getIDs();
                    for (int i=0;i<list3.size();i++){
                        manager_name=list3.get(i);
                    }
                    for (int i=0;i<list4.size();i++){
                        manager_id=list4.get(i);
                    }
                    if(list3.size()==1){
                        GetDock.dockingEntity3.setManager(manager_name);
                        GetDock.dockingEntity3.setManager_id(manager_id);
                        if (GetDock.dockingEntity3!=null){
                            finish();
                        }
                    }else if (list3.size()==0){
                        Toast.makeText(participationActivity.this, "请选择负责人哦！", Toast.LENGTH_SHORT).show();
                    }else  if (list3.size()>1) {
                        Toast.makeText(participationActivity.this, "只能选择一个负责人哦！", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
